<template>
  <a-row id="globalheader" align="center" :wrap="false">
    <a-col flex="auto">
      <div>
        <a-menu
          mode="horizontal"
          :selected-keys="selectedKeys"
          @menu-item-click="doMenuClick"
        >
          <a-menu-item
            key="0"
            :style="{ padding: 0, marginRight: '38px' }"
            disabled
          >
            <div class="title-bar">
              <img class="logo" src="../assets/R-C.jpg" />
              <div class="title">HKU OJ System</div>
            </div>
          </a-menu-item>
          <a-menu-item v-for="item in visibleRoutes" :key="item.path">
            {{ item.name }}
          </a-menu-item>
        </a-menu>
      </div>
    </a-col>
    <a-col flex="100px">
      <div v-if="store.state.user?.loginUser">
        {{ store.state.user?.loginUser?.userName }}
      </div>
      <div v-else>
        <a-span>
          <a-button
            type="primary"
            style="width: 80px; margin-left: 240px; border-radius: 100%"
            html-type="register"
            @click="toLogin()"
            >Sign In</a-button
          >
        </a-span>
      </div>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from "../router/routes";
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import ACCESS_ENUM from "@/access/accessEnum";

const router = useRouter();
//获取状态变量
const store = useStore();

//权限判断
const visibleRoutes = computed(() => {
  return routes.filter((item, index) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    if (!checkAccess(store.state.user.loginUser, item.meta?.access as string)) {
      return false;
    }
    return true;
  });
});

// setTimeout(() => {
//   store.dispatch("user/getLoginUser", {
//     userName: "gyf",
//     userRole: ACCESS_ENUM.ADMIN,
//   });
// }, 3000);

//路由跳转后更新选中的菜单项
router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});

//默认主页
const selectedKeys = ref(["/"]);

const toLogin = () => {
  router.push({
    path: "/user/login",
    // replace: true,
  });
};

//路由点击跳转事件
const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.logo {
  height: 48px;
  width: 48px;
}

.title {
  color: #444;
  margin-left: 16px;
}
</style>
